﻿using MyMvc.BLL;
using MyMvc.IBLL;
using MyMvc.Model;
using MyMvc.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BlueHome.Admin.Controllers
{
    public class SystemController : Controller
    {
        //
        // GET: /System/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ArticleType()
        {
            return View();
        }

        ICategoryTypeService _typeService = new CategoryTypeService();

        public ActionResult GetAllType()
        {
            //分页的数据
            //
            int pageIndex = Request["page"] == null ? 1 : int.Parse(Request["page"]);
            int pageSize = Request["rows"] == null ? 10 : int.Parse(Request["rows"]);

            //封装一个业务逻辑层，来处理分页过滤的事件
            GetModelQuery typeInfoQuery = new GetModelQuery();
            typeInfoQuery.pageIndex = pageIndex;
            typeInfoQuery.pageSize = pageSize;
            typeInfoQuery.total = 0;

            //放置依赖刷新
            var data = from u in _typeService.LoadSearchData(typeInfoQuery)
                       select new { u.ID,u.ParentId, u.TypeName };

            var result = new { total = typeInfoQuery.total, rows = data };

            return Json(result, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 添加类别信息
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public ActionResult AddType(CategoryType type)
        {
            //实现对CategoryType表的添加信息
            _typeService.AddEntities(type);
            return Content("OK");
        }
        /// <summary>
        /// 加载修改类别数据
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public ActionResult GetBindDetails(int ID)
        {
            var BindIDForUpdateTextBox = _typeService.LoadEntities(u => u.ID == ID).FirstOrDefault();
            return Json(BindIDForUpdateTextBox, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 修改类别信息
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public ActionResult UpdateInfo(CategoryType type)
        {
            _typeService.UpdateEntities(type);
            return Content("OK");
        }
        /// <summary>
        /// 删除类别信息
        /// </summary>
        /// <param name="deleteInfoID"></param>
        /// <param name="TName"></param>
        /// <returns></returns>
        public ActionResult DeleteInfo(string deleteInfoID, string TName)
        {
            if (string.IsNullOrEmpty(deleteInfoID))
            {
                return Content("请选择您要删除的数据");
            }
            //截取传递过来的字符串的数字信息
            var idsStr = deleteInfoID.Split(',');

            List<int> deleteIDList = new List<int>();

            foreach (var ID in idsStr)
            {
                deleteIDList.Add(Convert.ToInt32(ID));
            }
            if (_typeService.DeleteType(deleteIDList) > 0)
            {
                return Content("OK");
            }
            return Content("删除失败，请您检查");

        }
    }
}
